package controller.admin;

import model.Goods;
import model.Type;
import service.GoodsService;
import service.TypeService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "AdminGoodsEditShowServlet", value = "/admin/goods_editshow")
public class AdminGoodsEditShowServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        GoodsService gs = new GoodsService();
        TypeService ts = new TypeService();
        //商品id
        String goodsId = request.getParameter("id");
        String pageNumber = request.getParameter("pageNumber");
        String type = request.getParameter("type");
        String referer = request.getHeader("Referer");
        //没有得到想要的参数
        if (!(goodsId != null && !"".equals(goodsId))) {
            response.sendRedirect(referer);
            return;
        }
        try {

            Goods goods = gs.goods_detail(Integer.parseInt(goodsId));
            request.setAttribute("g", goods);
            request.setAttribute("pageNumber", pageNumber);
            request.setAttribute("type", type);
            List<Type> typeList = ts.getTypeList();
            request.setAttribute("typeList", typeList);
            request.getRequestDispatcher("goods_edit.jsp").forward(request, response);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
